<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Frontend_menu extends MX_Controller
	{
		private $module = "frontend_menu";
		
		public function __construct()
		{
			parent::__construct();
			$this->load->model($this->module.'_model','model');
			$this->load->library('pagination');
			
			$data_array = array('module' => $this->module);
			$this->template->set_template('admin');
			$this->template->write('title','Shopping Cart');
			$this->template->write_view('search','BACKEND/search',$data_array);
			$this->template->write_view('left','BACKEND/menu_left',$data_array);
			$this->template->write_view('footer','BACKEND/footer');
		}
		
		public function admin_index($start = 0,$limit = 10)
		{
			$config['total_rows'] = $this->model->admin_count();
			$config['base_url'] = base_url().'index.php/admin/'.$this->module.'/index/';
			$config['per_page'] = $limit;
			$config['num_links'] = 5;
			$config['uri_segment'] = 4;
			
			$this->pagination->initialize($config); 
			
			$result = $this->model->admin_index($config['per_page'],$start);
			
			$data = array(
				'module'	 => $this->module,
				'result'	 => $result
			);
			
			$this->template->write_view('content','BACKEND/index',$data);
			$this->template->render();
		}
		
		public function admin_save()
		{
			if($_POST)
			{				
			    if($this->model->admin_save())
			    {
					// Luu session de thong bao
				}
			}
			header('Location: '.base_url().'index.php/admin/'.$this->module.'/');
		}
		
		public function admin_sort()
		{
			$data = array();
			$sort   = $this->input->post('value');
			$result = $this->model->admin_sort($sort);
			
			if(!empty($result))
			{
				$data = array(
					'module' => $this->module,
					'result' => $result
				);
				$this->load->view('BACKEND/ajax_index',$data);
			}else{
				$data = array(
					'module' => $this->module,
					'result' => ''
				);
				$this->load->view('BACKEND/ajax_index',$data);
			}
		}
		
		public function admin_update($id = 0)
		{
			$result[0] = array();
			if($id != 0)
			{
				$result = $this->model->admin_update($id);
			}
	
			$data = array(
					'result'   	 	=> $result[0],
					'module'   	 	=> $this->module
			);
			
			$this->template->write_view('content','BACKEND/update',$data);
			$this->template->render();
		}
		
		public function admin_del($id = 0)
		{
			$id = intval($id);
			$this->model->admin_del($id);
			header('Location: '.base_url().'index.php/admin/'.$this->module.'/');
		}
		
		public function admin_update_status()
		{
			$id = $this->input->post('id');
			if($this->input->post('status') == 1)
			{
				$status = 0;
			}else{
				$status = 1;
			}
			
			$this->model->admin_update_status($id,$status);
			
			$data_update = array(
				'id'=>$id,
				'status'=>$status
			);
			
			$this->load->view('BACKEND/ajax_status',$data_update);
		}
		
		public function admin_del_all()
		{
			if($_POST)
			{
				$this->model->admin_del_all();
				header('Location: '.base_url().'index.php/admin/'.$this->module.'/');
			}else{
				header('Location: '.base_url().'index.php/admin/'.$this->module.'/');
			}
		}
		
		public function admin_search()
		{
			$data = array();
			if($_POST)
			{
				$result = $this->model->admin_search();
				$data = array(
					'module' => $this->module,
					'result' => $result
				);
				
				$this->template->write_view('content','BACKEND/index',$data);
				$this->template->render();
			}else{
				header('Location: '.base_url().'index.php/admin/'.$this->module.'/');
			}
		}
		
		/******** DYNAMIC MENU **********/
		// Input : Parent Menu
		// Output: List menu Parent-child menu
		private function _fillChildMenuList(&$parent_list)
		{
			if(!empty($parent_list))
			{
				for($i=0;$i<count($parent_list);$i++)
				{
					$parent_list[$i]['child_list'] = $this->model->_getChildMenu($parent_list[$i]['id']);
				}
			}
		}
	}
?>